// ywhw/pages/issue/issue.js
const app = getApp()
const db = wx.cloud.database()
const f = wx.cloud

const util = require('../../utils/util')

Page({

  /**
   * 页面的初始数据
   */
  data: {
      except:[],
      index: 0,
      category: ['书','衣物','食品','其他'],
     category_school:['北校区','南校区','西校区'],
      index_school:0,
      name:'',
      price:'',
      zg:false,
      yg:false,
      imgList:[],
      except_v:'',
      description:''
  },
  except_add(e){
      let a = this.data.except
      console.log(e)
      a.push(e.detail.value)
      this.setData({
        except:a
      })
  },
  // 修改状态
  checkChangeZg(){
    this.setData({
      zg:!this.data.zg
    })
  },
  checkChangeYg(){
    this.setData({
      yg:!this.data.yg
    })
  },
  // 重置表单数据
  formReset(){
    this.setData({
      except:[],
      index: 0,
      name:'',
      price:'',
      zg:false,
      yg:false,
      imgList:[],
      except_v:'',
      description:'',
      index_school:0
    })
  },
  PickerChange(e){
    this.setData({
      index:e.detail.value
    })
  },
  PickerChange_School(e){
    this.setData({
      index_school:e.detail.value
    })
  },
  // 获取表单数据并提交
  formSubmit:function(e){
    let value = e.detail.value
    value.category=this.data.category[value.category]
    value.category_school =this.data.category_school[value.category_school]
    value.except = this.data.except
      if(!value.name){
        wx.showToast({
          title: '请输入物品名',
          icon:'error'
        })
      }else if(!((this.data.yg&&value.except.length)||(this.data.zg&&value.price))){
        wx.showToast({
          title: '请至少选择一种易物方式',
          icon:'error'
        })
      }else if(this.data.except.length>3){
        wx.showToast({
          title: '易物最多三种',
          icon:'error'
        })
      }else{
          wx.showModal({
            title:'易物发布',
            content:'发布易物将在广场显示',
            success:res=>{
              if(res.confirm){
                let m =0
                  let fileId = []
                  this.data.imgList.forEach((item,index)=>{
                      f.uploadFile({
                        cloudPath:'yw/yw_commodity/'+util.formatTime(new Date())+Math.floor(9*Math.random())+'.jpg',
                        filePath: item
                      }).then(res=>{
                        m++
                          fileId.push(res.fileID)
                          if(m==this.data.imgList.length){
                            value.picture=fileId
                            value.userId=app.globalData.userInfo._openid
                            value.status = 'normal'
                            value.status_l = '等待中'
                            value.methods=''
                            value.createTime=util.formatTime(new Date())
                            value.chatName =app.globalData.userInfo.chatName
                            db.collection('yw_commodity').add({
                              data:value
                            }).then(()=>{
                              wx.showToast({
                                title: '发布成功',
                                complete:()=>{
                                  this.formReset()
                                }
                              })
                            })
                          }
                      })
                  })
              }else{
                console.log('取消')
              }
            },
          })
      }
  },
  ChooseImage(e){
      wx.chooseImage({
        count: 4,
        sizeType:['original','compressed'],
        sourceType:['album','caera'],
        success:res=>{
          if(this.data.imgList.length!=0){
              this.setData({
                imgList:this.data.imgList.concat(res.tempFilePaths)
              })
          }else{
              this.setData({
                imgList:res.tempFilePaths
              })
          }
          console.log(res)
        }
      })
  },
  DelImg(e){
    wx.showModal({
      title:'删除',
      content:'确定删除图片吗？',
      cancelText:'取消',
      confirmText:'确定',
      success:res=>{
        if(res.confirm){
          this.data.imgList.splice(e.currentTarget.dataset.index,1)
          this.setData({
            imgList:this.data.imgList
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  sc(e){
    let ex = this.data.except
    if(ex.length==1){
      ex.length=0
    }else{
      ex = ex.splice(e.currentTarget.dataset.index,1) 
    }
    this.setData({
      except:ex
    })
  }
})